package utils

import "strings"

// 生成驼峰命名规则的名称
func ToCamelName(name string) string {
	length := len(name)
	if length == 0 {
		return name
	}
	name = strings.TrimSpace(name)
	if strings.Contains(name, "-") {
		name = strings.Replace(name, "-", "_", -1)
	}
	if !strings.Contains(name, "_") {
		return name
	}
	camels := strings.Split(name, "_")
	var result string
	for _, camel := range camels {
		// 跳过原始字符串中开头、结尾的下换线或双重下划线
		runeList:=[]rune(camel)
		runeLength:=len(runeList)
		if runeLength == 0 {
			continue
		}
		// 处理真正的驼峰片段
		if len(result) == 0 {
			// 第一个驼峰片段，全部字母都小写
			result = strings.ToLower(camel)
		} else if runeLength <= 1 {
			result = result + strings.ToUpper(camel)
		} else {
			camel = strings.ToLower(camel)
			result = result + strings.ToUpper(camel[:1]) + strings.ToLower(camel[1:])
		}
	}
	return result
}

//Pascal 命名规则转换
func ToPascalName(name string) string {
	name = ToCamelName(name)
	if len(name) <= 1 {
		return name
	}
	name = strings.ToUpper(name[:1]) + name[1:]
	return name
}

//转为小写且去掉下划线
func ToLowername(name string) string {
	if len(name) == 0 {
		return name
	}
	name = strings.TrimSpace(name)
	name = strings.Replace(name, "_", "", -1)
	name = strings.Replace(name, "-", "", -1)
	return name
}
